package com.songwo.core.service.system.impl;

import com.songwo.core.primary.dao.system.SysUserDao;
import com.songwo.core.primary.entity.system.SysUser;
import com.songwo.core.service.system.SysUserService;
import com.songwo.util.RedisUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhongzheng
 */
@Service
@Log4j2
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private RedisUtils redisUtils;

    private final String USER_KEYS = "sysuser:list:allUsers";

    @Override
    public List<SysUser> list() {
        // 缓存中查询
        List<SysUser> sysUsers = (List<SysUser>) redisUtils.get(USER_KEYS);
        if (sysUsers == null) {
            // 数据库中查询
            sysUsers = sysUserDao.findAll();
            // 存入缓存
            redisUtils.save(USER_KEYS, sysUsers);
        }
        return sysUsers;
    }

    @Override
    public SysUser findSysUserByName(String name) {
        return sysUserDao.findSysUserByName(name);
    }

//    @Autowired
//    @Qualifier("primaryJdbcTemplate")
//    protected JdbcTemplate jdbcTemplate1;
//
//    @Autowired
//    @Qualifier("secondaryJdbcTemplate")
//    protected JdbcTemplate jdbcTemplate2;
//
//    @Override
//    public void create(String name, Integer age) {
//        jdbcTemplate1.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
//    }
//
//    @Override
//    public void deleteByName(String name) {
//        jdbcTemplate1.update("delete from USER where NAME = ?", name);
//    }
//
//    @Override
//    public Integer getAllUsers() {
//        return jdbcTemplate1.queryForObject("select count(1) from USER", Integer.class);
//    }
//
//    @Override
//    public void deleteAllUsers() {
//        jdbcTemplate1.update("delete from USER");
//    }
}
